package com.xinghou.platform.dao.main.aspect;

import com.xinghou.platform.model.main.InterestTags;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import org.aspectj.lang.ProceedingJoinPoint;

import com.xinghou.platform.dao.main.common.DBUserRelationLevelCached;
import com.xinghou.platform.model.main.UserRelationFrom;
import com.xinghou.platform.util.JsonUtil;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class UserRelationFromCacheAspect extends DBUserRelationLevelCached {
    @Pointcut("execution(public * com.xinghou.platform.dao.main.UserRelationFromDAO.getUserRelationFromByUserId(..))")
    public void getUserRelationFromByUserId() {
    }

    @Pointcut("execution(public * com.xinghou.platform.dao.main.UserRelationFromDAO.updateUserRelationFrom(..))")
    public void updateUserRelationFrom() {
    }

    @Pointcut("execution(public * com.xinghou.platform.dao.main.UserRelationFromDAO.saveUserRelationFrom(..))")
    public void saveUserRelationFrom() {
    }


    @Pointcut("execution(public * com.xinghou.platform.dao.main.UserRelationFromDAO.getFollowCountByUserid(..))")
    public void getFollowCountByUserid() {
    }

    @Pointcut("execution(public * com.xinghou.platform.dao.main.UserRelationFromDAO.getFansCountByUserid(..))")
    public void getFansCountByUserid() {
    }


    public Boolean isAttention(Integer fromuid, Integer touid) {
        String keyInfoId = String.format(CACHE_URFROM_USERID, fromuid, touid);
        if (getRedisClient().exists(keyInfoId, CACHED_DB_INDEX)) {
            String jsonToken = getRedisClient().get(keyInfoId, CACHED_DB_INDEX);
            UserRelationFrom userRelationFrom = (UserRelationFrom) JsonUtil.fromJson(jsonToken.toString(), UserRelationFrom.class);
            if (userRelationFrom.getIsliked() == 1)
                return true;
            else
                return false;
        }
        return null;
    }


    public void praise(Integer fromuid, Integer touid) {
        String key = String.format(CACHE_USER_FOLLOWCOUNT, fromuid);
        if (getRedisClient().exists(key, CACHED_DB_INDEX)) {
            getRedisClient().incr(key, CACHED_DB_INDEX);
        }
        String key1 = String.format(CACHE_USER_FANCOUNT, touid);
        if (getRedisClient().exists(key1, CACHED_DB_INDEX)) {
            getRedisClient().incr(key1, CACHED_DB_INDEX);
        }
    }

    public void cancel(Integer fromuid, Integer touid) {
        String key = String.format(CACHE_USER_FOLLOWCOUNT, fromuid);
        if (getRedisClient().exists(key, CACHED_DB_INDEX)) {
            String v= getRedisClient().get(key,CACHED_DB_INDEX);
            if(Integer.parseInt(v)>=1) {
                getRedisClient().decr(key, CACHED_DB_INDEX);
            }
        }
        String key1 = String.format(CACHE_USER_FANCOUNT, touid);
        if (getRedisClient().exists(key1, CACHED_DB_INDEX)) {
            String v= getRedisClient().get(key1,CACHED_DB_INDEX);
            if(Integer.parseInt(v)>=1) {
                getRedisClient().decr(key1, CACHED_DB_INDEX);
            }
        }
    }


    @Around("getFollowCountByUserid()")
    public int getFollowCountByUserid(ProceedingJoinPoint pjp) throws Throwable {

        String fromUserId = ((Object[]) pjp.getArgs())[0].toString();
        String key = String.format(CACHE_USER_FOLLOWCOUNT, fromUserId);
        Object jsonToken = getRedisClient().get(key, CACHED_DB_INDEX);
        if (jsonToken != null) {
            return Integer.parseInt(jsonToken.toString());
        } else {
            int v = (int) pjp.proceed();
            getRedisClient().set(key, v + "", CACHED_DB_INDEX);
            return v;
        }
    }

    @Around("getFansCountByUserid()")
    public int getFansCountByUserid(ProceedingJoinPoint pjp) throws Throwable {

        String fromUserId = ((Object[]) pjp.getArgs())[0].toString();
        String key = String.format(CACHE_USER_FANCOUNT, fromUserId);
        Object jsonToken = getRedisClient().get(key, CACHED_DB_INDEX);
        if (jsonToken != null) {
            return Integer.parseInt(jsonToken.toString());
        } else {
            int v = (int) pjp.proceed();
            getRedisClient().set(key, v + "", CACHED_DB_INDEX);
            return v;
        }
    }

    @Around("getUserRelationFromByUserId()")
    public UserRelationFrom getUserRelationFromByUserId(ProceedingJoinPoint pjp) throws Throwable {
        String fromUserId = ((Object[]) pjp.getArgs())[0].toString();
        String toUserId = ((Object[]) pjp.getArgs())[1].toString();
        String key = String.format(CACHE_URFROM_USERID, fromUserId, toUserId);
        Object jsonToken = getRedisClient().get(key, CACHED_DB_INDEX);
        if (jsonToken == null) {
            UserRelationFrom userRelationFrom = (UserRelationFrom) pjp.proceed();
            if (userRelationFrom != null)
                getRedisClient().set(key, JsonUtil.toJson(userRelationFrom), CACHED_DB_INDEX);
            return userRelationFrom;
        } else {
            UserRelationFrom userRelationFrom = (UserRelationFrom) JsonUtil.fromJson(jsonToken.toString(), UserRelationFrom.class);
            return userRelationFrom;
        }
    }

    @Around("saveUserRelationFrom()")
    public int saveUserRelationFrom(ProceedingJoinPoint pjp) throws Throwable {
        UserRelationFrom userRelationFrom = (UserRelationFrom) ((Object[]) pjp.getArgs())[0];
        String keyInfoId = String.format(CACHE_URFROM_USERID, userRelationFrom.getFromuserid(), userRelationFrom.getTouserid());
        if (getRedisClient().exists(keyInfoId, CACHED_DB_INDEX)) {
            Object jsonToken = getRedisClient().get(keyInfoId, CACHED_DB_INDEX);
            UserRelationFrom temp = (UserRelationFrom) JsonUtil.fromJson(jsonToken.toString(), UserRelationFrom.class);
            if (userRelationFrom.getIsliked() == temp.getIsliked())
                throw new ServiceException(DBExceptionEnums.HAS_ATTENTION);
        }
        getRedisClient().set(keyInfoId, JsonUtil.toJson(userRelationFrom), CACHED_DB_INDEX);
        int returnno = (int) pjp.proceed();
        if (userRelationFrom.getIsliked() == 1) {
            praise(userRelationFrom.getFromuserid(), userRelationFrom.getTouserid());
        } else {
            cancel(userRelationFrom.getFromuserid(), userRelationFrom.getTouserid());
        }
        return returnno;
    }

    @Around("updateUserRelationFrom()")
    public int updateUserRelationFrom(ProceedingJoinPoint pjp) throws Throwable {
        UserRelationFrom userRelationFrom = (UserRelationFrom) ((Object[]) pjp.getArgs())[0];
        String keyInfoId = String.format(CACHE_URFROM_USERID, userRelationFrom.getFromuserid(), userRelationFrom.getTouserid());
        if (getRedisClient().exists(keyInfoId, CACHED_DB_INDEX)) {
            Object jsonToken = getRedisClient().get(keyInfoId, CACHED_DB_INDEX);
            UserRelationFrom temp = (UserRelationFrom) JsonUtil.fromJson(jsonToken.toString(), UserRelationFrom.class);
            if (userRelationFrom.getIsliked() == 1 && temp.getIsliked() == 1)
                throw new ServiceException(DBExceptionEnums.HAS_ATTENTION);
            if (userRelationFrom.getIsliked() == 0 && temp.getIsliked() == 0)
                throw new ServiceException(DBExceptionEnums.HAS_CANCELATTENTION);
        }
        getRedisClient().set(keyInfoId, JsonUtil.toJson(userRelationFrom), CACHED_DB_INDEX);
        int returnno = (int) pjp.proceed();
        if (userRelationFrom.getIsliked() == 1) {
            praise(userRelationFrom.getFromuserid(), userRelationFrom.getTouserid());
        } else {
            cancel(userRelationFrom.getFromuserid(), userRelationFrom.getTouserid());
        }
        return returnno;
    }

}
